package com.bw.unit3.view.activity

import android.widget.ImageView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.alipay.sdk.app.PayTask
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bw.unit3.Const.Companion.PATH_GOODS_DETAIL
import com.bw.unit3.R
import com.bw.unit3.base.BaseActivity
import com.bw.unit3.databinding.ActivityGoodsDetailBinding
import com.bw.unit3.viewmodel.GoodsDetailViewModel
import kotlin.concurrent.thread

@Route(path = PATH_GOODS_DETAIL)
class GoodsDetailActivity :
    BaseActivity<GoodsDetailViewModel, ActivityGoodsDetailBinding>(R.layout.activity_goods_detail) {
    @JvmField
    @Autowired
    var goodsId: Int = 0

    override fun initData() {
        viewModel.liveDataGoods.observe(this) {
            dataBinding.banner.setData(it.data.bannerList, null)
            dataBinding.banner.setAutoPlayTime(1200)
            dataBinding.banner.setAutoPlayAble(true)
            dataBinding.banner.setmAdapter { _, _, view, position ->
                Glide.with(this@GoodsDetailActivity).load(it.data.bannerList[position])
                    .into(view as ImageView)
            }

            //设置商品详情信息
            //商品价格
            dataBinding.tvPrice.text = "¥ ${it.data.goods_default_price}"
            //商品描述
            dataBinding.tvDesc.text = it.data.goods_desc
            //商品销量
            dataBinding.tvSale.text = "月销售 ${it.data.goods_sales_count}"
        }
    }

    override fun initView() {
        //ARouter参数注入配置
        ARouter.getInstance().inject(this)
        //根据goodsId获取详情内容
        viewModel.getGoodsDeail(goodsId)

        //加入购物车
        dataBinding.tvCart.setOnClickListener {
            //调接口加入购物车，加入成功后，跳转到购物车列表
            val map = HashMap<String, String>()
            map["count"] = "1"
            map["goods_id"] = goodsId.toString()
            viewModel.addCart(map)
        }

        //立即购买
        dataBinding.tvBuy.setOnClickListener {
            //调用支付宝sdk，沙箱环境模拟支付

            toPay()
            //支付宝对接地址

        }

        dataBinding.ivBack.setOnClickListener {
            finish()
        }


    }

    private fun toPay() {
        val orderInfo =
            "app_id=2015052600090779&biz_content=%7B%22timeout_express%22%3A%2230m%22%2C%22seller_id%22%3A%22%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22total_amount%22%3A%220.02%22%2C%22subject%22%3A%221%22%2C%22body%22%3A%22%E6%88%91%E6%98%AF%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE%22%2C%22out_trade_no%22%3A%22314VYGIAGG7ZOYY%22%7D&charset=utf-8&method=alipay.trade.app.pay&sign_type=RSA2&timestamp=2016-08-15%2012%3A12%3A15&version=1.0&sign=MsbylYkCzlfYLy9PeRwUUIg9nZPeN9SfXPNavUCroGKR5Kqvx0nEnd3eRmKxJuthNUx4ERCXe552EV9PfwexqW%2B1wbKOdYtDIb4%2B7PL3Pc94RZL0zKaWcaY3tSL89%2FuAVUsQuFqEJdhIukuKygrXucvejOUgTCfoUdwTi7z%2BZzQ%3D" // 订单信息
        // 必须异步调用
        val payRunnable = Runnable {
            val alipay = PayTask(this@GoodsDetailActivity)
            val result = alipay.payV2(orderInfo, true)
            runOnUiThread {
                if (result != null)
                    ToastUtils.showShort("支付成功")
            }
        }
        // 必须异步调用
        // 必须异步调用
        val payThread = Thread(payRunnable)
        payThread.start()
    }

}